{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "46263917-8e41-4c25-a518-76e44f5d9ee1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def now():\n",
    "    print('2024-6-1')\n",
    "f = now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f5e2e71-2496-4845-b3ab-8dda76f1f2c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-6-1\n"
     ]
    }
   ],
   "source": [
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ea206160-8031-49e7-bf00-4500fda323e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'now'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a694f84d-4eb5-4eec-8b1f-32b698a422fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(func): \n",
    "    def wrapper(*args, **kw): \n",
    "        print('call %s(): ' % func.__name__)\n",
    "        return func(*args, **kw) \n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c610ad58-2315-421e-b4dd-00d2d36aaaaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "@log \n",
    "def now():\n",
    "    print('2025-06-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4d3846a9-875f-45d1-bc11-cec837e5ee41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call now(): \n",
      "2025-06-01\n"
     ]
    }
   ],
   "source": [
    "now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "87a176a1-7c20-42ca-8d8e-f8699df8776f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(text): \n",
    "    def decorator(func):\n",
    "        def wrapper(*args, **kw): \n",
    "            print('%s %s(): ' % (text, func.__name__))\n",
    "            return func(*args, **kw) \n",
    "        return wrapper\n",
    "    return decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "75c66f02-bdc1-4eb3-b29a-f268bc763c10",
   "metadata": {},
   "outputs": [],
   "source": [
    "@log('execute')\n",
    "def now():\n",
    "    print('2024-06-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "56c03df8-4045-4cbb-9e98-bda566c1bf6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute now(): \n",
      "2024-06-01\n"
     ]
    }
   ],
   "source": [
    "now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6f3a4de5-6425-4504-8268-496ca11dbf5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'wrapper'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "92bff05b-693b-418a-835d-e071880802ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "\n",
    "def log(text):\n",
    "    def decorator(func):\n",
    "        @functools.wraps(func)\n",
    "        def wrapper(*args, **kw):\n",
    "            print('%s %s():' % (text, func.__name__))\n",
    "            return func(*args, **kw)\n",
    "        return wrapper\n",
    "    return decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a4abecb5-f787-4401-8655-56c4ff681e70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute now():\n",
      "2024-07-01\n"
     ]
    }
   ],
   "source": [
    "@log('execute')\n",
    "def now():\n",
    "    print('2024-07-01')\n",
    "t = now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "16269702-1064-4b6f-b408-b90826bf8af5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'now'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "214fa4f9-b78e-42a9-82b4-893455d6c293",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from functools import wraps\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f96454d3-a9eb-400f-96c5-c30f7cb57f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "def metric(fn):\n",
    "    @wraps(fn)\n",
    "    def wrapper(*args, **kw):\n",
    "        first = time.time()\n",
    "        fn(*args, **kw)\n",
    "        end = time.time() \n",
    "        print('%s executed in %s ms' % (fn.__name__, end-first))\n",
    "        return fn(*args, **kw)\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8abc698d-83db-4af9-a844-97e5d3880a27",
   "metadata": {},
   "outputs": [],
   "source": [
    "@metric\n",
    "def fast(x, y):\n",
    "    time.sleep(0.35)\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "abefa99f-4f6a-4658-8e32-3329333f2fce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fast executed in 0.3566405773162842 ms\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "f = fast(11, 22)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a3b85839-5b0b-4f57-b35b-73881f5912c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(*outsiders):\n",
    "    def decorator(func):\n",
    "        @functools.wraps(func)\n",
    "        def wrapper(*args, **kw):\n",
    "            inpts = []\n",
    "            for n in outsiders:\n",
    "                inpts.append(n)\n",
    "            print('%s %s():' % (inpts, func.__name__))\n",
    "            return func(*args, **kw)\n",
    "        return wrapper\n",
    "    return decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "853e9ddb-87b1-4a15-8914-25415c83c67b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['execute'] now():\n",
      "2024-07-01\n"
     ]
    }
   ],
   "source": [
    "@log('execute')\n",
    "def now():\n",
    "    print('2024-07-01')\n",
    "t = now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dd10692e-8cdc-4f43-9ee5-f0e39b020b6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] now():\n",
      "2024-07-01\n"
     ]
    }
   ],
   "source": [
    "@log()\n",
    "def now():\n",
    "    print('2024-07-01')\n",
    "t = now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8685c67c-2d26-4f5e-8aa6-eb16d3dd8b03",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
